<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="visible"
		:width="screenWidth"
		:fullscreen="mobile === 'mobile'"
    :title="dialogAddOrEdit.title"
    draggable
    @close="closeDialog"
		close-icon="CloseBold"
	>
    <el-form
      ref="addOrEditFormRef"
      :model="formData"
      :rules="rules"
			v-loading="loading"
      label-width="100"
      size=""
			:disabled="dialogAddOrEdit.detail === '详情'"
    >
      <el-row justify="start" class="">
        <el-col :span="itemSpan" class="">
          <el-form-item label="命令名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入命令名称" />
          </el-form-item>
        </el-col>

<!--        <el-col :span="itemSpan" class="">-->
<!--          <el-form-item label="设备分类" prop="deviceType">-->
<!--						<el-select-->
<!--							v-model="formData.deviceType"-->
<!--							placeholder="请选择设备分类"-->
<!--							filterable-->
<!--							clearable-->
<!--						>-->
<!--							<el-option-->
<!--								v-for="item in ProjectCheckInDeviceType"-->
<!--								:key="item.value"-->
<!--								:label="item.label"-->
<!--								:value="item.value"-->
<!--							/>-->
<!--						</el-select>-->
<!--          </el-form-item>-->
<!--        </el-col>-->

<!--        <el-col :span="itemSpan" class="">-->
<!--          <el-form-item label="命令代码" prop="operator">-->
<!--						<el-input v-model="formData.operator" placeholder="请输入命令代码" />-->
<!--          </el-form-item>-->
<!--        </el-col>-->

        <el-col :span="itemSpan" class="">
          <el-form-item label="命令编码" prop="code">
						<el-input v-model="formData.code" placeholder="请输入命令编码" />
          </el-form-item>
        </el-col>
	
				<el-col :span="itemSpan" class="">
					<el-form-item label="是否自动执行" prop="isAuto">
						<el-select
							v-model="formData.isAuto"
							placeholder="请选择是否自动执行"
							filterable
							clearable
						>
							<el-option
								:value="true"
								label="是"
							></el-option>
							<el-option
								:value="false"
								label="否"
							></el-option>
						</el-select>
					</el-form-item>
				</el-col>
	
				<el-col :span="itemSpan" class="">
					<el-form-item label="是否禁用" prop="isDisable">
						<el-select
							v-model="formData.isDisable"
							placeholder="请选择是否禁用"
							filterable
							clearable
						>
							<el-option
								:value="true"
								label="是"
							></el-option>
							<el-option
								:value="false"
								label="否"
							></el-option>
						</el-select>
					</el-form-item>
				</el-col>

      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{
          dialogAddOrEdit.footBtn
        }}</el-button>
        <el-button @click="closeDialog()">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {ref, reactive, computed} from "vue";
import {
	addCheckInDeviceCommand,
	editCheckInDeviceCommand,
	getCheckInDeviceCommandInfo
} from "@/api/system/device/index.ts";
import {CheckinDeviceCommandForm} from "@/api/system/device/types";
import { getDicts } from "@/store/modules/dict";

import {useScreen} from "@/store/modules/screen";

const screenStore = useScreen()
const mobile = computed(() => ( screenStore.device ));
const screenWidth = ref(900)
const itemSpan = ref(12)

function initScreen(){
	if(mobile.value === 'mobile'){
		screenWidth.value = screenStore.screenWidth;
		itemSpan.value = 24;
	}else {
		screenWidth.value = 900;
		itemSpan.value = 12;
	}
}

const props = defineProps({
  dialogAddOrEdit: {
    type: Object,
    default: () => ({
      visible: false,
      title: "新增设备信息",
      footBtn: "新增",
      id: 0,
    }),
  },
});

const emit = defineEmits(["sendClose"]);

const loading = ref(false);
let visible = ref(props.dialogAddOrEdit.visible);
const formData = reactive<CheckinDeviceCommandForm>({});
const addOrEditFormRef = ref(ElForm);

const rules = reactive<FormRules<CheckinDeviceCommandForm>>({
	name: [{ required: true, message: "请输入命令名称", trigger: "blur" }],
	// deviceType: [{ required: true, message: "请选择设备分类", trigger: "blur" }],
	// operator: [{ required: true, message: "请输入命令代码", trigger: "blur" }],
	isDisable: [{ required: true, message: "请选择是否禁用", trigger: "blur" }],
	code: [{ required: true, message: "请输入命令编码", trigger: "blur" }],
});

onMounted(async () => {
	initScreen()
	
  let tempFormData;
  if (props.dialogAddOrEdit.id) {
		loading.value = true;
    tempFormData = await getCheckInDeviceCommandInfo(props.dialogAddOrEdit.id);
    tempFormData = tempFormData.data;
    Object.assign(formData, tempFormData);
		loading.value = false;
    tempFormData = null;
  }
});

/* 发送当前弹窗关闭 */
function sendClose(query) {
  emit("sendClose",query);
}

/**
 * 关闭弹窗
 */
function closeDialog(query) {
  visible.value = false;
  sendClose(query);
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();

  formData.id = undefined;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
				editCheckInDeviceCommand(id, formData)
          .then((resp) => {
            if (resp.success === true) {
              ElMessage.success(resp.msg);
              closeDialog(true);
							loading.value = false;
            } else {
              ElMessage.warning(resp.msg);
							loading.value = false;
            }
          })
      } else {
				addCheckInDeviceCommand(formData)
          .then((resp) => {
            if (resp.success === true) {
              ElMessage.success(resp.msg);
              closeDialog(true);
							loading.value = false;
            } else {
              ElMessage.warning(resp.msg);
							loading.value = false;
            }
          })
      }
    }
  });
}
const DeviceType = getDicts("deviceType"); //设备分类
const ProjectCheckInDeviceType = getDicts("projectCheckInDeviceType"); //银行类别
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 0px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}
.dialog_footer {
  // display: flex;
  // justify-content: center;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
	width: 100%;
}

:deep(.el-input__wrapper) {
	width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
